package com.ngc.mall.controller;

import com.ngc.mall.common.configure.LocalUser;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.core.enumeration.PayType;
import com.ngc.mall.core.enumeration.PlatformType;
import com.ngc.mall.domain.OrderDomain;
import com.ngc.mall.dto.OrderDTO;
import com.ngc.mall.logic.OrderChecker;
import com.ngc.mall.param.OrderParam;
import com.ngc.mall.service.IOrderService;
import com.ngc.mall.vo.OrderCountVO;
import com.ngc.mall.vo.PagingVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private IOrderService iOrderService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/test")
    public ResponseResult<Boolean> test(){
        stringRedisTemplate.opsForValue().set("cxpqq", "1", 20, TimeUnit.SECONDS);
        return ResponseResult.auto(true);
    }
    @GetMapping("/test/get")
    public ResponseResult<Boolean> testGet(){
        String value=stringRedisTemplate.opsForValue().get("cxpqq");
        System.out.println(value);
        return ResponseResult.auto(true);
    }


    @PostMapping("")
    @ApiOperation("创建订单")
    public ResponseResult<Long> createOrder(@RequestBody OrderParam orderParam){
        Long uid= LocalUser.getUser().getId();
        OrderDTO orderDTO= OrderDTO.builder().orderAddressDTO(orderParam.getOrderAddressDTO()).skuInfoDTOList(orderParam.getSkuInfoDTOList()).totalAmount(orderParam.getTotalAmount()).finalPayAmount(orderParam.getFinalPayAmount()).note(orderParam.getNote()).couponId(orderParam.getCouponId()).sourceType(PlatformType.MINI_PROGRAM.getValue()).payType(PayType.WECHAT.getValue()).build();
        OrderChecker orderChecker=iOrderService.isOk(orderDTO,uid);
        Long orderId=iOrderService.createOrder(orderDTO,uid,orderChecker);
        return ResponseResult.success(orderId);
    }


    @GetMapping("/by/status/{status}")
    @ApiOperation("获取个人各状态的订单")
    public ResponseResult<PagingVO<OrderDomain>> getByStatus(@PathVariable int status,
                                                @RequestParam(name = "start", defaultValue = "0")
                                                                Integer start,
                                                @RequestParam(name = "count", defaultValue = "8")
                                                                      Integer count){
            return ResponseResult.success(iOrderService.getMySelfByStatus(status,start,count));
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id获取订单详情")
    public ResponseResult<OrderDomain> getDetailById(@PathVariable Long id){
        return ResponseResult.success(iOrderService.getOrderDetailById(id));
    }


    @PutMapping("/status/receive/{oid}")
    @ApiOperation("确定订单已完成")
    public ResponseResult<Boolean> updateStatusToReceive(@PathVariable Long oid){
        Long uid=LocalUser.getUser().getId();
        return ResponseResult.success(iOrderService.updateStatusToReceive(uid,oid));
    }

    @GetMapping("/my/count/by/status")
    @ApiOperation("获取个人各状态订单")
    public ResponseResult<OrderCountVO> getMyOrderCountByStatus(){
        Long uid=LocalUser.getUser().getId();
//        Long uid=Long.valueOf("1679736432946819074");
        return ResponseResult.success(iOrderService.getMyOrderCountByStatus(uid));
    }

}
